package com.gitee.huxing.course.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.gitee.huxing.course.order.domain.module.PlatFormOrderDO;
import com.gitee.huxing.course.order.service.PlatFormOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.controller
 * @ClassName: TestController
 * @Author: huxing
 * @DateTime: 2021-09-17 下午4:32
 */
@RestController
public class TestController {

    @Resource
    PlatFormOrderService service;

    @RequestMapping("/api/v1/placeOrder")
    public String generalOrder() throws RuntimeException{
        PlatFormOrderDO orderDO = this.service.purchase();
         return Optional.ofNullable(orderDO).isPresent() ?  JSONObject.toJSONString(orderDO) :  "下单失败";
    }

    @RequestMapping("/api/v1/getOrder/{orderId}")
    public String queryOrder(@PathVariable("orderId") String orderId) throws RuntimeException{
        PlatFormOrderDO platFormOrderDO = this.service.queryOrder(orderId);
        return Optional.ofNullable(platFormOrderDO).isPresent() ? "您好，您的订单记录为: " + JSONObject.toJSONString(this.service.queryOrder(orderId))
                : "未查询到订单记录";
    }

    @RequestMapping("/api/v1/update/{orderId}")
    public String update(@PathVariable("orderId") String orderId){
        return "订单更新成功, 您的订单记录为: " +  this.service.updateOrder(orderId);
    }

    @RequestMapping("/api/v1/delete/{orderId}")
    public String delete(@PathVariable("orderId") String orderId){
        this.service.deleteOrder(orderId);
        return "您的订单已删删除";
    }

    @RequestMapping("/api/v1/batchPlaceOrder")
    public String testBatch() throws RuntimeException{
        long startTime = System.currentTimeMillis();
        this.service.batchPurchase();
        long endTime = System.currentTimeMillis();
        return "批量下单成功, 总计耗时: " + (endTime-startTime) + "ms";
    }

    @RequestMapping("/api/v1/batchPlaceOrderTrans/{page}")
    public String testBatch(@PathVariable("page") String page) throws RuntimeException{
        long startTime = System.currentTimeMillis();
        this.service.batchPurchaseTrans(Integer.parseInt(page));
        long endTime = System.currentTimeMillis();
        return "批量下单成功, 总计耗时: " + (endTime-startTime) + "ms";
    }
}
